package costamt;

import java.awt.Desktop;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileFilter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.text.BadLocationException;

public class Start extends javax.swing.JFrame {

    public static ArrayList<String> directoryfiles = new ArrayList();
    public static ArrayList<String> filemt = new ArrayList();
    public static ArrayList<String> fileref = new ArrayList();
    public static ArrayList<String> filesor = new ArrayList();
    protected int flag; //flag 1 create, flag 0 continue
    String username = "";
    String systemname = "";
    String l1 = "";
    String l2 = "";
    String Segm = "";
    int segments = 0;
    String mtname;
    String sorname;
    String refname;
    String sysfiletemp;
    String dbfiletemp;
    String resfiletemp;
    String continuefilename;

    public Start() {

        initComponents();
        this.setLocationRelativeTo(null);
        jTextFieldUser.setEnabled(false);
        jTextFieldUser1.setEnabled(false);
        jTextFieldUser2.setEnabled(false);
        jTextFieldUser3.setEnabled(false);
        jTextFieldUser4.setEnabled(false);
        jButton1.setEnabled(false);
        jButton3.setEnabled(false);
        jButton4.setEnabled(false);
        jButton5.setEnabled(false);
        jLabel1.setEnabled(false);
        jLabel2.setEnabled(false);
        jLabel3.setEnabled(false);
        jLabel4.setEnabled(false);
        jLabel5.setEnabled(false);
        jLabel7.setEnabled(false);
        jLabel8.setEnabled(false);
        jLabel9.setEnabled(false);
        jTextField1.setEnabled(false);
        jTextField2.setEnabled(false);
        jTextField3.setEnabled(false);
        jLabel11.setEnabled(false);
        jLabel12.setEnabled(false);
        jLabel13.setEnabled(false);
        jLabel14.setEnabled(false);
        jLabel15.setEnabled(false);

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jToggleButton1 = new javax.swing.JToggleButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jTextFieldUser = new javax.swing.JTextField();
        jTextFieldUser1 = new javax.swing.JTextField();
        jTextFieldUser2 = new javax.swing.JTextField();
        jTextFieldUser3 = new javax.swing.JTextField();
        jTextFieldUser4 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jButton4 = new javax.swing.JButton();
        jTextField3 = new javax.swing.JTextField();
        jButton5 = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();
        jButton7 = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        jLabel15 = new javax.swing.JLabel();
        jButton8 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton9 = new javax.swing.JButton();

        jToggleButton1.setText("jToggleButton1");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("COSTA MT Evaluation Tool");

        jLabel1.setText("Annotator's Name");
        jLabel1.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);

        jLabel2.setText("MT System's Name");
        jLabel2.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);

        jLabel3.setText("Source Language");
        jLabel3.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);

        jLabel4.setText("Target Language");
        jLabel4.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);

        jLabel5.setText("Number of Segments");
        jLabel5.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 30)); // NOI18N
        jLabel6.setForeground(new java.awt.Color(0, 153, 255));
        jLabel6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/logo64.png"))); // NOI18N
        jLabel6.setText("COSTA MT Evaluation Tool ");

        jTextFieldUser.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldUserActionPerformed(evt);
            }
        });

        jTextFieldUser4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldUser4ActionPerformed(evt);
            }
        });

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/start.png"))); // NOI18N
        jButton1.setText("Start Evaluation");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/open_file.png"))); // NOI18N
        jButton3.setToolTipText("Add File");
        jButton3.setMaximumSize(new java.awt.Dimension(25, 25));
        jButton3.setMinimumSize(new java.awt.Dimension(25, 25));
        jButton3.setPreferredSize(new java.awt.Dimension(25, 25));
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });

        jButton4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/open_file.png"))); // NOI18N
        jButton4.setToolTipText("Add File");
        jButton4.setMaximumSize(new java.awt.Dimension(25, 25));
        jButton4.setMinimumSize(new java.awt.Dimension(25, 25));
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jButton5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/open_file.png"))); // NOI18N
        jButton5.setToolTipText("Add File");
        jButton5.setMaximumSize(new java.awt.Dimension(25, 25));
        jButton5.setMinimumSize(new java.awt.Dimension(25, 25));
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        jButton6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/continue_project.png"))); // NOI18N
        jButton6.setText("Continue an existing project");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });

        jButton7.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/new_project.png"))); // NOI18N
        jButton7.setText("Create a new project");
        jButton7.setMaximumSize(new java.awt.Dimension(219, 33));
        jButton7.setMinimumSize(new java.awt.Dimension(219, 33));
        jButton7.setPreferredSize(new java.awt.Dimension(191, 25));
        jButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
        });

        jLabel7.setText("Select Source file");

        jLabel8.setText("Select MT file");

        jLabel9.setText("Select Reference file");

        jLabel11.setText("e.g. Moses");

        jLabel12.setText("e.g. John Smith");

        jLabel13.setText("e.g. English");

        jLabel14.setText("e.g. Greek");

        jLabel15.setText("e.g. 200");

        jButton8.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/info_small.png"))); // NOI18N
        jButton8.setToolTipText("About COSTA MT Evaluation Tool");
        jButton8.setMaximumSize(new java.awt.Dimension(30, 30));
        jButton8.setMinimumSize(new java.awt.Dimension(30, 30));
        jButton8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton8ActionPerformed(evt);
            }
        });

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/question_faq.png"))); // NOI18N
        jButton2.setToolTipText("How to use COSTA MT Evaluation Tool");
        jButton2.setMaximumSize(new java.awt.Dimension(30, 30));
        jButton2.setMinimumSize(new java.awt.Dimension(30, 30));
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton9.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/puzzle.png"))); // NOI18N
        jButton9.setToolTipText("Documentation");
        jButton9.setMaximumSize(new java.awt.Dimension(30, 30));
        jButton9.setMinimumSize(new java.awt.Dimension(30, 30));
        jButton9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton9ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(56, 56, 56)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jButton1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel9)
                        .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(jButton6)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 219, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addComponent(jLabel7))
                                .addGap(15, 15, 15)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jTextFieldUser1, javax.swing.GroupLayout.PREFERRED_SIZE, 234, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(10, 10, 10)
                                        .addComponent(jLabel12))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jTextFieldUser2, javax.swing.GroupLayout.PREFERRED_SIZE, 234, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(10, 10, 10)
                                        .addComponent(jLabel13))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jTextFieldUser, javax.swing.GroupLayout.PREFERRED_SIZE, 234, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(10, 10, 10)
                                        .addComponent(jLabel11))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 234, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(10, 10, 10)
                                        .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jTextFieldUser3, javax.swing.GroupLayout.PREFERRED_SIZE, 234, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(10, 10, 10)
                                        .addComponent(jLabel14))
                                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 234, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jTextFieldUser4, javax.swing.GroupLayout.PREFERRED_SIZE, 234, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(10, 10, 10)
                                        .addComponent(jLabel15))
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                            .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 234, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addGap(10, 10, 10)
                                            .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                            .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap(56, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButton9, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(5, 5, 5)
                .addComponent(jButton8, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(5, 5, 5)
                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(20, 20, 20))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jButton8, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton9, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel6)
                .addGap(40, 40, 40)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(53, 53, 53)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldUser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel11))
                .addGap(12, 12, 12)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldUser1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel12))
                .addGap(12, 12, 12)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldUser2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(jLabel13))
                .addGap(12, 12, 12)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldUser3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(jLabel14))
                .addGap(12, 12, 12)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jTextFieldUser4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel15))
                    .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.TRAILING))
                .addGap(12, 12, 12)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel7))
                        .addGap(12, 12, 12)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel8)
                            .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(12, 12, 12)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel9)
                            .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(67, 67, 67)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(77, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jTextFieldUserActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldUserActionPerformed
    }//GEN-LAST:event_jTextFieldUserActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed

        jTextField1.setEditable(false);
        sorname = selectTextFile();
        jTextField1.setText(sorname);

    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed

        jTextField2.setEditable(false);
        mtname = selectTextFile();
        jTextField2.setText(mtname);
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        jTextField3.setEditable(false);
        refname = selectTextFile();
        jTextField3.setText(refname);
    }//GEN-LAST:event_jButton5ActionPerformed

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed

        flag = 0;

        jTextFieldUser.setEditable(false);
        jTextFieldUser1.setEditable(false);
        jTextFieldUser2.setEditable(false);
        jTextFieldUser3.setEditable(false);
        jTextFieldUser4.setEditable(false);


        jLabel1.setEnabled(true);
        jLabel2.setEnabled(true);
        jLabel3.setEnabled(true);
        jLabel4.setEnabled(true);
        jLabel5.setEnabled(true);
        jTextField1.setEnabled(false);
        jTextField2.setEnabled(false);
        jTextField3.setEnabled(false);
        jButton1.setEnabled(true);
        jButton3.setEnabled(false);
        jButton4.setEnabled(false);
        jButton5.setEnabled(false);
        jLabel7.setEnabled(false);
        jLabel8.setEnabled(false);
        jLabel9.setEnabled(false);
        jButton1.setEnabled(false);

        directoryfiles.clear();

        File folder = new File(".");
        File[] listOfFiles = folder.listFiles(filefilter);

        for (File file : listOfFiles) {
            if (file.isFile()) {
                try {
                    BufferedReader reader = new BufferedReader(new FileReader(file.getName()));
                    try {
                        systemname = reader.readLine();
                        username = reader.readLine();
                        l1 = reader.readLine();
                        l2 = reader.readLine();
                        Segm = reader.readLine();
                        reader.close();
                        directoryfiles.add("<html><i>System:</i> " + "<b>" + systemname + "</b>" + "  " + "<i>Annotator:</i> " + "<b>" + username + "</b>" + "  " + "<i>From:</i> " + "<b>" + l1 + "</b>" + "  " + "<i>To:</i> " + "<b>" + l2 + "</b></html>");
                        directoryfiles.add(file.getName());
                    } catch (IOException ex) {
                        Logger.getLogger(Start.class.getName()).log(Level.SEVERE, null, ex);
                    }
                } catch (FileNotFoundException e) {
                }
            }
        }

        String bigList[] = new String[directoryfiles.size() / 2];

        for (int lk = 0; lk < directoryfiles.size(); lk = lk + 2) {
            bigList[lk / 2] = directoryfiles.get(lk);
        }

        JOptionPane pane = new JOptionPane();
        Object ans;
        ans = pane.showInputDialog(null, "Select a project", "Message", JOptionPane.QUESTION_MESSAGE, null, bigList, "");

        if (ans != null) {
            for (int lk = 0; lk < directoryfiles.size(); lk = lk + 2) {
                if (ans.toString().equals(directoryfiles.get(lk))) {

                    continuefilename = directoryfiles.get(lk + 1);
                }
            }

            File f = new File(continuefilename);
            if (f.exists()) {
                try {
                    BufferedReader reader = new BufferedReader(new FileReader(f));
                    try {
                        systemname = reader.readLine();
                        username = reader.readLine();
                        l1 = reader.readLine();
                        l2 = reader.readLine();
                        Segm = reader.readLine();
                        reader.close();
                    } catch (IOException ex) {
                        Logger.getLogger(Start.class.getName()).log(Level.SEVERE, null, ex);
                    }
                } catch (FileNotFoundException e) {
                }
            }

            jTextFieldUser.setEnabled(true);
            jTextFieldUser1.setEnabled(true);
            jTextFieldUser2.setEnabled(true);
            jTextFieldUser3.setEnabled(true);
            jTextFieldUser4.setEnabled(true);

            jTextFieldUser.setText(systemname);
            jTextFieldUser1.setText(username);
            jTextFieldUser2.setText(l1);
            jTextFieldUser3.setText(l2);
            jTextFieldUser4.setText(Segm);
            jButton1.setEnabled(true);
        }
    }//GEN-LAST:event_jButton6ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        if (flag == 1) {

            filemt.clear();
            fileref.clear();
            filesor.clear();
            try {
                ReadTxt.ReadTxt(filemt, mtname);
                ReadTxt.ReadTxt(fileref, refname);
                ReadTxt.ReadTxt(filesor, sorname);
            } catch (Exception ex1) {
                Logger.getLogger(Form.class.getName()).log(Level.SEVERE, null, ex1);
            }

            if (jTextFieldUser.getText().length() < 4) {
                String message = "MT System's Name must be at least 4 characters long";
                JOptionPane.showMessageDialog(this, message);
            } else if (jTextFieldUser.getText().contains("/") || jTextFieldUser.getText().contains("*") || jTextFieldUser.getText().contains("?")
                    || jTextFieldUser.getText().contains("\\") || jTextFieldUser.getText().contains("<") || jTextFieldUser.getText().contains(">")
                    || jTextFieldUser.getText().contains("|") || jTextFieldUser.getText().contains("\"") || jTextFieldUser.getText().contains(":")) {
                String message = "MT System's Name cannot contain any of the following characters: \\ / : * ? \" < > |";
                JOptionPane.showMessageDialog(this, message);
            } else if (jTextFieldUser1.getText().length() < 4) {
                String message = "Annotator's Name must be at least 4 characters long";
                JOptionPane.showMessageDialog(this, message);
            } else if (jTextFieldUser1.getText().contains("/") || jTextFieldUser1.getText().contains("*") || jTextFieldUser1.getText().contains("?")
                    || jTextFieldUser1.getText().contains("\\") || jTextFieldUser1.getText().contains("<") || jTextFieldUser1.getText().contains(">")
                    || jTextFieldUser1.getText().contains("|") || jTextFieldUser1.getText().contains("\"") || jTextFieldUser1.getText().contains(":")) {
                String message = "Annotator's Name cannot contain any of the following characters: \\ / : * ? \" < > |";
                JOptionPane.showMessageDialog(this, message);
            } else if (jTextFieldUser2.getText().length() < 2) {
                String message = "Source Language must be at least 2 characters long";
                JOptionPane.showMessageDialog(this, message);
            } else if (jTextFieldUser2.getText().contains("/") || jTextFieldUser2.getText().contains("*") || jTextFieldUser2.getText().contains("?")
                    || jTextFieldUser2.getText().contains("\\") || jTextFieldUser2.getText().contains("<") || jTextFieldUser2.getText().contains(">")
                    || jTextFieldUser2.getText().contains("|") || jTextFieldUser2.getText().contains("\"") || jTextFieldUser2.getText().contains(":")) {
                String message = "Source Language cannot contain any of the following characters: \\ / : * ? \" < > |";
                JOptionPane.showMessageDialog(this, message);
            } else if (jTextFieldUser3.getText().length() < 2) {
                String message = "Target Language must be at least 2 characters long";
                JOptionPane.showMessageDialog(this, message);
            } else if (jTextFieldUser3.getText().contains("/") || jTextFieldUser3.getText().contains("*") || jTextFieldUser3.getText().contains("?")
                    || jTextFieldUser3.getText().contains("\\") || jTextFieldUser3.getText().contains("<") || jTextFieldUser3.getText().contains(">")
                    || jTextFieldUser3.getText().contains("|") || jTextFieldUser3.getText().contains("\"") || jTextFieldUser3.getText().contains(":")) {
                String message = "Target Language cannot contain any of the following characters: \\ / : * ? \" < > |";
                JOptionPane.showMessageDialog(this, message);
            } else if (!jTextFieldUser4.getText().matches(".*\\d.*")) {
                String message = "Segments must contain only digits";
                JOptionPane.showMessageDialog(this, message);
            } else if (jTextField1.getText().length() == 0) {
                String message = "Please select Source file!";
                JOptionPane.showMessageDialog(this, message);
            } else if (jTextField2.getText().length() == 0) {
                String message = "Please select MT file!";
                JOptionPane.showMessageDialog(this, message);
            } else if (jTextField3.getText().length() == 0) {
                String message = "Please select Reference Translations!";
                JOptionPane.showMessageDialog(this, message);
            } else if (!(filemt.size() == filesor.size() && filesor.size() == fileref.size())) {
                String message = "The size of files differs";
                JOptionPane.showMessageDialog(this, message);
            } else {
                startEvaluation();
            }
        } else {
            startEvaluation();
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed
        flag = 1;

        jTextFieldUser.setEnabled(true);
        jTextFieldUser1.setEnabled(true);
        jTextFieldUser2.setEnabled(true);
        jTextFieldUser3.setEnabled(true);
        jTextFieldUser4.setEnabled(true);

        jTextFieldUser.setEditable(true);
        jTextFieldUser1.setEditable(true);
        jTextFieldUser2.setEditable(true);
        jTextFieldUser3.setEditable(true);
        jTextFieldUser4.setEditable(true);
        jTextFieldUser.setText("");
        jTextFieldUser1.setText("");
        jTextFieldUser2.setText("");
        jTextFieldUser3.setText("");
        jTextFieldUser4.setText("");
        jTextFieldUser4.setText("0");
        jButton1.setEnabled(true);
        jButton3.setEnabled(true);
        jButton4.setEnabled(true);
        jButton5.setEnabled(true);
        jLabel1.setEnabled(true);
        jLabel2.setEnabled(true);
        jLabel3.setEnabled(true);
        jLabel4.setEnabled(true);
        jLabel5.setEnabled(true);
        jLabel7.setEnabled(true);
        jLabel8.setEnabled(true);
        jLabel9.setEnabled(true);
        jTextField1.setEnabled(true);
        jTextField1.setEditable(false);
        jTextField2.setEnabled(true);
        jTextField2.setEditable(false);
        jTextField3.setEnabled(true);
        jTextField3.setEditable(false);
        jLabel11.setEnabled(true);
        jLabel12.setEnabled(true);
        jLabel13.setEnabled(true);
        jLabel14.setEnabled(true);
        jLabel15.setEnabled(true);
    }//GEN-LAST:event_jButton7ActionPerformed

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed
        
    }//GEN-LAST:event_jTextField1ActionPerformed

    private void jTextFieldUser4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldUser4ActionPerformed
           
    }//GEN-LAST:event_jTextFieldUser4ActionPerformed

    private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton8ActionPerformed
        String message =
                "<html><b>COSTA MT Evaluation Tool</b></html> \n"
                + "<html><i>An Open Toolkit for human Machine Translation Evaluation</i></html> \n\n"
                + "Beta version \n"
                + "url: https://code.google.com/p/costa-mt-evaluation-tool \n \n"
                + "Copyright © 2013 Konstantinos CHATZITHEODOROU, Stamatis CHATZISTAMATIS  \n \n"
                + "E-mail: dinosaxon@gmail.com\n\n"
                + "This is free software, licensed under the terms of the GNU General Public License v3.\n \n"
                + "This is beta software and we welcome all feedback.";
        JOptionPane.showMessageDialog(this, message);
    }//GEN-LAST:event_jButton8ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        String message =
                "<html><b>COSTA MT Evaluation Tool</b> requires the user to provide three plain text files (UTF-8).</html>"
                + "\nEach line of these files should contain one sentence.  \n \n"
                + "<html>1. <i><b>Source file</i></b> contains the source sentences,</html> \n"
                + "<html>2. <i><b>MT file</i></b> contains the candidate translations,</html>\n"
                + "<html>3. <i><b>Reference file</i></b> contains the reference translations.</html>\n \n"
                + "<html><b>COSTA MT Evaluation Tool</b> gives the opportunity to the user to choose the number of sentences</html>"
                + "\nand to interrupt/restart the evaluation task at any time.";
        JOptionPane.showMessageDialog(this, message);

    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton9ActionPerformed
        if (Desktop.isDesktopSupported()) {

            File myFile = new File("Docs/Documentation.pdf");
            File myFile2 = new File("Docs/README.txt");
            if (myFile.exists()) {
                try {
                    Desktop.getDesktop().open(myFile);
                } catch (IOException ex) {
                    Logger.getLogger(Start.class.getName()).log(Level.SEVERE, null, ex);
                }
            } else if (myFile2.exists()) {
                try {
                    Desktop.getDesktop().open(myFile2);
                } catch (IOException ex) {
                    Logger.getLogger(Start.class.getName()).log(Level.SEVERE, null, ex);
                }
            } else {
                String message = "For documentation, please visit: https://code.google.com/p/costa-mt-evaluation-tool";
                JOptionPane.showMessageDialog(this, message);
            }
        }
    }//GEN-LAST:event_jButton9ActionPerformed
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Start.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Start.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Start.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Start.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Start().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JButton jButton8;
    private javax.swing.JButton jButton9;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextFieldUser;
    private javax.swing.JTextField jTextFieldUser1;
    private javax.swing.JTextField jTextFieldUser2;
    private javax.swing.JTextField jTextFieldUser3;
    private javax.swing.JTextField jTextFieldUser4;
    private javax.swing.JToggleButton jToggleButton1;
    // End of variables declaration//GEN-END:variables

    public static String selectTextFile() {
        do {
            JFileChooser chooser = new JFileChooser();
            FileNameExtensionFilter filter = new FileNameExtensionFilter(
                    "Text files", "txt");
            chooser.setFileFilter(filter);
            int returnVal = chooser.showOpenDialog(null);

            if (returnVal == JFileChooser.APPROVE_OPTION) {
                return chooser.getSelectedFile().toString();
            } else {
                return null;
            }
        } while (true);
    }

    public void startEvaluation() {

        int dblines = 0;
        if (flag == 1) //create new project
        {

            File f = null;
            try {
                sysfiletemp = jTextFieldUser.getText(0, 4) + "_" + jTextFieldUser1.getText(0, 4) + "_" + jTextFieldUser2.getText(0, 2) + "_" + jTextFieldUser3.getText(0, 2) + "_sys";
                f = new File(sysfiletemp);
            } catch (BadLocationException ex) {
                Logger.getLogger(Start.class.getName()).log(Level.SEVERE, null, ex);
            }
            if (!f.exists()) {
                try {
                    f.createNewFile();

                    BufferedWriter b = new BufferedWriter(new FileWriter(f));
                    try {
                        b.write(jTextFieldUser.getText());
                        b.newLine();
                        b.write(jTextFieldUser1.getText());
                        b.newLine();
                        b.write(jTextFieldUser2.getText());
                        b.newLine();
                        b.write(jTextFieldUser3.getText());
                        b.newLine();
                        b.write(jTextFieldUser4.getText());
                        b.newLine();
                        b.write(jTextField1.getText());
                        b.newLine();
                        b.write(jTextField2.getText());
                        b.newLine();
                        b.write(jTextField3.getText());
                        b.flush();
                        b.close();
                    } catch (IOException ex) {
                        Logger.getLogger(Start.class.getName()).log(Level.SEVERE, null, ex);
                    }
                } catch (FileNotFoundException e) {
                } catch (IOException ex) {
                    Logger.getLogger(Start.class.getName()).log(Level.SEVERE, null, ex);
                }

                File db = null;
                try {
                    dbfiletemp = jTextFieldUser.getText(0, 4) + "_" + jTextFieldUser1.getText(0, 4) + "_" + jTextFieldUser2.getText(0, 2) + "_" + jTextFieldUser3.getText(0, 2) + "_db";
                    db = new File(dbfiletemp);
                } catch (BadLocationException ex2) {
                    Logger.getLogger(Start.class.getName()).log(Level.SEVERE, null, ex2);
                }
                try {
                    db.createNewFile();
                } catch (IOException ex3) {
                    Logger.getLogger(Start.class.getName()).log(Level.SEVERE, null, ex3);
                }
                File res = null;
                try {
                    resfiletemp = jTextFieldUser.getText(0, 4) + "_" + jTextFieldUser1.getText(0, 4) + "_" + jTextFieldUser2.getText(0, 2) + "_" + jTextFieldUser3.getText(0, 2) + "_results";
                    res = new File(resfiletemp);
                } catch (BadLocationException ex5) {
                    Logger.getLogger(Start.class.getName()).log(Level.SEVERE, null, ex5);
                }
                try {
                    res.createNewFile();
                } catch (IOException ex7) {
                    Logger.getLogger(Start.class.getName()).log(Level.SEVERE, null, ex7);
                }
                try {
                    BufferedWriter bdb = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(db), "UTF8"));
                    segments = Integer.parseInt(jTextFieldUser4.getText());
                    if (segments < filemt.size() && segments > 0) {
                        dblines = segments;
                    } else {
                        dblines = filemt.size();
                    }

                    bdb.write(filesor.get(0));

                    bdb.newLine();
                    bdb.write(filemt.get(0));
                    bdb.newLine();
                    bdb.write(fileref.get(0));

                    for (int t = 1; t < dblines; t++) {
                        bdb.newLine();
                        bdb.write(filesor.get(t));
                        bdb.newLine();
                        bdb.write(filemt.get(t));
                        bdb.newLine();
                        bdb.write(fileref.get(t));

                    }
                    bdb.flush();
                    bdb.close();
                    this.dispose();
                    new Form(sysfiletemp, dbfiletemp, resfiletemp).setVisible(true);

                } catch (IOException ex4) {
                    Logger.getLogger(Start.class.getName()).log(Level.SEVERE, null, ex4);
                }

            } else {
                String message = "This Evaluation already exists. Please Select Continue an existing project!";
                JOptionPane.showMessageDialog(this, message);
                
            }


        } else // continue 
        {
              try {
                sysfiletemp = jTextFieldUser.getText(0, 4) + "_" + jTextFieldUser1.getText(0, 4) + "_" + jTextFieldUser2.getText(0, 2) + "_" + jTextFieldUser3.getText(0, 2) + "_sys";
            } catch (BadLocationException ex) {
                Logger.getLogger(Start.class.getName()).log(Level.SEVERE, null, ex);
            }

            try {
                dbfiletemp = jTextFieldUser.getText(0, 4) + "_" + jTextFieldUser1.getText(0, 4) + "_" + jTextFieldUser2.getText(0, 2) + "_" + jTextFieldUser3.getText(0, 2) + "_db";
            } catch (BadLocationException ex2) {
                Logger.getLogger(Start.class.getName()).log(Level.SEVERE, null, ex2);
            }
            try {
                resfiletemp = jTextFieldUser.getText(0, 4) + "_" + jTextFieldUser1.getText(0, 4) + "_" + jTextFieldUser2.getText(0, 2) + "_" + jTextFieldUser3.getText(0, 2) + "_results";
                //  res = new File(resfiletemp);
            } catch (BadLocationException ex5) {
                Logger.getLogger(Start.class.getName()).log(Level.SEVERE, null, ex5);
            }

            this.dispose();
            new Form(sysfiletemp, dbfiletemp, resfiletemp).setVisible(true);
        }
    }

    FileFilter filefilter = new FileFilter() {
        public boolean accept(File file) {
            if (file.getName().endsWith("_sys")) {
                return true;
            }
            return false;
        }
    };
}